package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysWebsiteConfig;
import com.ruoyi.system.service.ISysWebsiteConfigService;

@RestController
@RequestMapping("/system/website/config")
public class SysWebsiteConfigController extends BaseController {
    @Autowired
    private ISysWebsiteConfigService websiteConfigService;

    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysWebsiteConfig config) {
        startPage();
        List<SysWebsiteConfig> list = websiteConfigService.selectConfigList(config);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId) {
        return success(websiteConfigService.selectConfigById(configId));
    }

    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/key/{configKey}")
    public AjaxResult getConfigByKey(@PathVariable String configKey) {
        return success(websiteConfigService.selectConfigByKey(configKey));
    }

    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "网站配置管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysWebsiteConfig config) {
        config.setCreateBy(SecurityUtils.getUsername());
        return toAjax(websiteConfigService.insertConfig(config));
    }

    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "网站配置管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysWebsiteConfig config) {
        config.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(websiteConfigService.updateConfig(config));
    }

    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "网站配置管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(websiteConfigService.deleteConfigByIds(configIds));
    }

    // 添加一个前台获取配置的接口（不需要权限验证）
    @Anonymous
    @GetMapping("/frontend")
    public AjaxResult getFrontendConfig() {
        SysWebsiteConfig queryParam = new SysWebsiteConfig();
        List<SysWebsiteConfig> configs = websiteConfigService.selectConfigList(queryParam);
        return success(configs);
    }
}